package com.example.controller;

import com.example.common.RequirePermission;
import com.example.common.Result;
import com.example.dto.OperationLogQueryDTO;
import com.example.service.OperationLogService;
import com.example.vo.OperationLogDetailVO;
import com.example.vo.OperationLogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 操作日志控制器
 */
@RestController
@RequestMapping("/api/operation-log")
@CrossOrigin
public class OperationLogController {
    
    @Autowired
    private OperationLogService operationLogService;
    
    /**
     * 分页查询操作日志
     */
    @PostMapping("/page")
    @RequirePermission("admin:log:view")
    @com.example.common.OperationLog(
        module = "日志管理",
        operationType = "QUERY",
        description = "分页查询操作日志"
    )
    public Result<Map<String, Object>> getOperationLogs(@RequestBody OperationLogQueryDTO queryDTO) {
        return operationLogService.getOperationLogs(queryDTO);
    }
    
    /**
     * 查询操作日志详情
     */
    @GetMapping("/{id}")
    @RequirePermission("admin:log:view")
    @com.example.common.OperationLog(
        module = "日志管理",
        operationType = "QUERY",
        description = "查询操作日志详情"
    )
    public Result<OperationLogDetailVO> getOperationLogDetail(@PathVariable Long id) {
        return operationLogService.getOperationLogDetail(id);
    }
    
    /**
     * 删除操作日志
     */
    @DeleteMapping("/{id}")
    @RequirePermission("admin:log:delete")
    @com.example.common.OperationLog(
        module = "日志管理",
        operationType = "DELETE",
        description = "删除操作日志"
    )
    public Result<Void> deleteOperationLog(@PathVariable Long id) {
        return operationLogService.deleteOperationLog(id);
    }
    
    /**
     * 批量删除操作日志
     */
    @DeleteMapping("/batch")
    @RequirePermission("admin:log:delete")
    @com.example.common.OperationLog(
        module = "日志管理",
        operationType = "DELETE",
        description = "批量删除操作日志"
    )
    public Result<Void> batchDeleteOperationLogs(@RequestBody List<Long> ids) {
        return operationLogService.batchDeleteOperationLogs(ids);
    }
    
    /**
     * 查询用户操作日志
     */
    @GetMapping("/user/{userId}")
    @RequirePermission("admin:log:view")
    @com.example.common.OperationLog(
        module = "日志管理",
        operationType = "QUERY",
        description = "查询用户操作日志"
    )
    public Result<List<OperationLogVO>> getUserOperationLogs(@PathVariable Long userId,
                                                             @RequestParam(defaultValue = "10") Integer limit) {
        return operationLogService.getUserOperationLogs(userId, limit);
    }
    
    /**
     * 查询模块操作日志
     */
    @GetMapping("/module/{module}")
    @RequirePermission("admin:log:view")
    @com.example.common.OperationLog(
        module = "日志管理",
        operationType = "QUERY",
        description = "查询模块操作日志"
    )
    public Result<List<OperationLogVO>> getModuleOperationLogs(@PathVariable String module,
                                                               @RequestParam(defaultValue = "10") Integer limit) {
        return operationLogService.getModuleOperationLogs(module, limit);
    }
    
    /**
     * 查询失败日志
     */
    @GetMapping("/failures")
    @RequirePermission("admin:log:view")
    @com.example.common.OperationLog(
        module = "日志管理",
        operationType = "QUERY",
        description = "查询失败日志"
    )
    public Result<List<OperationLogVO>> getFailureLogs(@RequestParam(required = false) LocalDateTime startTime,
                                                       @RequestParam(required = false) LocalDateTime endTime,
                                                       @RequestParam(defaultValue = "50") Integer limit) {
        return operationLogService.getFailureLogs(startTime, endTime, limit);
    }
    
    /**
     * 获取操作统计
     */
    @GetMapping("/statistics")
    @RequirePermission("admin:log:view")
    @com.example.common.OperationLog(
        module = "日志管理",
        operationType = "QUERY",
        description = "获取操作统计"
    )
    public Result<Map<String, Object>> getOperationStatistics(@RequestParam(required = false) LocalDateTime startTime,
                                                              @RequestParam(required = false) LocalDateTime endTime) {
        return operationLogService.getOperationStatistics(startTime, endTime);
    }
    
    /**
     * 获取今日统计
     */
    @GetMapping("/today-statistics")
    @RequirePermission("admin:log:view")
    @com.example.common.OperationLog(
        module = "日志管理",
        operationType = "QUERY",
        description = "获取今日统计"
    )
    public Result<Map<String, Object>> getTodayStatistics() {
        return operationLogService.getTodayStatistics();
    }
    
    /**
     * 导出操作日志
     */
    @PostMapping("/export")
    @RequirePermission("admin:log:export")
    @com.example.common.OperationLog(
        module = "日志管理",
        operationType = "EXPORT",
        description = "导出操作日志"
    )
    public Result<String> exportOperationLogs(@RequestBody OperationLogQueryDTO queryDTO) {
        return operationLogService.exportOperationLogs(queryDTO);
    }
    
    /**
     * 清理历史日志
     */
    @PostMapping("/clean")
    @RequirePermission("admin:log:clean")
    @com.example.common.OperationLog(
        module = "日志管理",
        operationType = "DELETE",
        description = "清理历史日志"
    )
    public Result<Integer> cleanHistoryLogs(@RequestParam Integer days) {
        return operationLogService.cleanHistoryLogs(days);
    }
    
    /**
     * 获取用户操作趋势
     */
    @GetMapping("/user-trend/{userId}")
    @RequirePermission("admin:log:view")
    @com.example.common.OperationLog(
        module = "日志管理",
        operationType = "QUERY",
        description = "获取用户操作趋势"
    )
    public Result<Map<String, Object>> getUserOperationTrend(@PathVariable Long userId,
                                                             @RequestParam(defaultValue = "7") Integer days) {
        return operationLogService.getUserOperationTrend(userId, days);
    }
}
